package com.tang.algorithm.doublePointer;

/**
 * @author tlm
 * @Description 环形链表
 * @date 2020/7/20 8:59 下午
 */
public class LeetCode141 {
	/**
	 * 快慢指针法 如果链表有环，则快指针最终会追上满指针。
	 * @param head
	 * @return
	 */
	public boolean hasCycle(ListNode head) {
		if (head == null || head.next == null) {
			return false;
		}
		ListNode slow = head;
		ListNode fast = head.next;
		while (slow != fast) {
			if (fast == null || fast.next == null) {
				return false;
			}
			fast = fast.next.next;
			slow = slow.next;
		}
		return true;
	}
}

class ListNode {
	int val;
	ListNode next;

	ListNode(int x) {
		val = x;
	}
}
